package com.example.springbootgraduation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootgraduation.entity.Vaccine;
import com.example.springbootgraduation.service.VaccineService;
import com.example.springbootgraduation.vo.DataView;
import com.example.springbootgraduation.vo.VaccineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*疫苗*/
@Controller
@RequestMapping("/vaccine")
public class VaccineController {

    @Autowired
    private VaccineService vaccineService;

    /**
     * 跳转疫苗页面
     */
    @RequestMapping("/toVaccine")
    public String toHeSuan(){
        return "vaccine/vaccine";
    }

    /**
     * 查询所有记录
     * @param
     * @return
     */
    @RequestMapping("/loadAllVaccine")
    @ResponseBody
    public DataView loadAllHeSuan(VaccineVo vaccineVo){
        IPage<Vaccine> page = new Page<>(vaccineVo.getPage(),vaccineVo.getLimit());
        QueryWrapper<Vaccine> queryWrapper = new QueryWrapper();
        vaccineService.page(page,queryWrapper);
        return new DataView(page.getTotal(),page.getRecords());
    }

    /**
     * 新增
     */
    @RequestMapping("/addVaccine")
    @ResponseBody
    public DataView addHeSuan(Vaccine vaccine){
        vaccineService.save(vaccine);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("添加疫苗成功！");
        return  dataView;
    }

    /**
     * 修改
     */
    @RequestMapping("/updateVaccine")
    @ResponseBody
    public DataView updateHeSuan(Vaccine vaccine){
        vaccineService.updateById(vaccine);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("修改疫苗成功！");
        return  dataView;
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteVaccine")
    @ResponseBody
    public DataView deleteHeSuan(Integer id){
        vaccineService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除疫苗成功！");
        return  dataView;
    }
}
